package com.xy.springboot.start.run;

import cn.hutool.core.net.Ipv4Util;
import cn.hutool.core.net.NetUtil;
import com.iohao.game.bolt.broker.client.AbstractBrokerClientStartup;
import com.iohao.game.bolt.broker.core.common.IoGameGlobalConfig;
import com.iohao.game.bolt.broker.server.BrokerServer;
import com.iohao.game.external.core.ExternalServer;
import com.iohao.game.external.core.netty.simple.NettyRunOne;
import com.xy.springboot.game.external.GameExternal;
import com.xy.springboot.game.task.start.GameLogicTaskLogic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.xy.springboot.game.broker.GameBrokerBoot;

import java.util.Arrays;
import java.util.List;
@Slf4j
@Component
@Order(1) // 控制执行顺序
public class GameRunner implements CommandLineRunner {
    // 对外开放的端口
    private final int externalPort = 10100;
    private final int brokerPort = IoGameGlobalConfig.brokerPort;
    @Override
    public void run(String... args) {
        String localIp = NetUtil.getLocalhostStr();
        log.info("游戏开始启动中，Ip：{},对外端口：{}、网关端口：{}",localIp,externalPort, brokerPort);
        // 游戏对外服
        ExternalServer externalServerWebSocket = new GameExternal().createExternalServer(externalPort);
        // broker （游戏网关）
        BrokerServer brokerServer = new GameBrokerBoot().createBrokerServer(brokerPort);
        // 多服单进程的方式部署（类似单体应用）
        new NettyRunOne()
                // broker （游戏网关）
                .setBrokerServer(brokerServer)
                // 游戏对外服
//                .setExternalServerList(List.of(externalServerWebSocket, externalServerTcp))
                .setExternalServer(externalServerWebSocket)
                // 游戏逻辑服列表
                .setLogicServerList(this.listLogic())
                // 启动 游戏对外服、游戏网关、游戏逻辑服
                .startup();
    }
    /**
     * 逻辑服列表
     * @return 逻辑服列表
     */
    private  List<AbstractBrokerClientStartup> listLogic() {
        return Arrays.asList(
                // 创建逻辑服
                new GameLogicTaskLogic()
        );
    }
}
